package com.gylang.im.core.util;

import com.gylang.commons.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gylang
 * email 1179346492@qq.com
 * data 2020/5/30
 * @version v0.0.1
 */
public class ParamBuilder {

    private final Map<String, String> param;

    private ParamBuilder() {
        param = new HashMap<>();

    }

    private ParamBuilder(int initSize) {
        param = new HashMap<>(initSize);
    }

    public static ParamBuilder builder() {
        return new ParamBuilder();
    }

    public static ParamBuilder builder(int initSize) {
        initSize = (int) (((float) initSize) / 0.75 + 1.0);
        return new ParamBuilder(initSize);
    }

    public ParamBuilder add(String key, String value) {

        param.put(key, value);
        return this;
    }

    public ParamBuilder addNotEmpty(String key, String value) {

        if (StringUtils.isNotBlank(value)) {
            param.put(key, value);
        }
        return this;
    }

    public String buildJson() {
        return JsonUtils.toJson(this.param);
    }

    public String buildForm() {

        StringBuilder formParam = new StringBuilder();
        for (Map.Entry<String, String> entry : param.entrySet()) {
            formParam.append(entry.getKey()).append("=").append(entry.getKey()).append("&");
        }
        return formParam.deleteCharAt(formParam.length() - 1).toString();
    }

    public Map<String, Object> buildMap() {
        return new HashMap<>(this.param);
    }
    public Map<String, String> buildStrMap() {
        return this.param;
    }



}
